<?php

defined('IN_YuLin') || exit('NO PERMIT!');
UserLogin(U($m.'/'.$c.'/'.$a));

if($a == 'opentime')
{
    $id = intval($_POST['lotteryname']);
    $lottery = $db->getrow('select * from '.table('lottery').' where ptid = "ssc" and id = "'.$id.'"');
    if($lottery)
    {
        $info = $db->getrow('select * from '.table('lottery_k3').' where tid = '.$lottery['id'].' and starttime <= '.time().' and stoptime >= '.time().'');
        $data = [];
        $data['lastFullExpect'] = $info['period'];
        $data['currFullExpect'] = $info['period'] + 1;
        $data['remainTime'] = $info['stoptime'] - time();
        $data['shortname'] = $lottery['title'];
        $data['status'] = 1;
        $data['message'] = 'OK';
        $info = array('status'=>true,'data'=>$data);
       // AjaxReturn(0,'获取数据成功',[$data]);
        echo json_encode($info);
    }
    exit;
}
if($a == 'returnbl')
{
	if(IS_POST)
	{
		$id = trim($_POST['id']);
		$money = intval($_POST['money']);
		$t = $db->getrow('SELECT bl FROM '.Table('lottery_type').' WHERE val = "'.$id.'" AND type = "k3"');
		echo number_format($t['bl'] * $money,2);
		exit;
	}	
}
if($a == 'countnum3')
{
    if(IS_POST)
    {
        $str = trim($_POST['str'],',');
        $num = intval($_POST['num']);
        $arr = explode(',',$str);

        $n = getSequenceAry($arr,$num);
        $new = 0;
        foreach($n as $key=>$val)
        {
            if(count($val) == '3')
            {
                $new += 1;
            }
        }

        echo $new;
        exit;
    }
}
if($a == 'countnum2')
{
    if(IS_POST)
    {
        $str = trim($_POST['str'],',');
        $num = intval($_POST['num']);
        $arr = explode(',',$str);

        $n = getSequenceAry($arr,$num);
        $new = 0;
        foreach($n as $key=>$val)
        {
            if(count($val) == '2')
            {
                $new += 1;
            }
        }

        echo $new;
        exit;
    }
}
if($a == 'orderAdd')
{
    if(IS_POST)
    {
        $cid = intval($_POST['cid']);
        !$cid && AjaxReturn(1,'请选择彩票种类');
        
        $iid = intval($_POST['iid']);
        !$iid && AjaxReturn(1,'请选择彩票期号');

        $data = $_POST['BettingData'];
        !is_array($data) && AjaxReturn(1,'请选择要投注的号码');
        foreach($data as $key=>$val)
        {
            $tmp = [];
            $tmp['uid']     =   $_uid;
            $tmp['tid']     =   'order_add';
            $tmp['lkey']    =   'order_add_'.$_uid.'_'.$cid.date('_YmdHis');
            $tmp['money']   =   $money;
            $tmp['balance'] =   -$money;
            $tmp['notes']   =   '下注购买彩票：'.$game['title'];
            $res = MoneyLog($tmp);
            !$res && AjaxReturn(1,'账户余额不足，请先充值');

            $orderno = date('YmdHis').str_pad($cid,2,0,STR_PAD_LEFT).str_pad($_uid,6,0,STR_PAD_LEFT).rand(1000,9999);
            $tmp = [];
            $tmp['uid']     =   $_uid;
            $tmp['tid']     =   $cid;
            $tmp['period']  =   $val['betting_number'];
            $tmp['orderno'] =   $orderno;
            $tmp['money'] =   $val['betting_money'];
            $tmp['status']   =   '0';
            $tmp['createtime']=   TIMESTAMP;
            $orderid = $db->add('lottery_k3_order',$tmp);
        } 
        AjaxReturn(0,'下注成功！');
    }
}

$ssc = $db->getall('select * from '.table('lottery').' where ptid = "ssc"');

$info = $db->getrow('select * from '.table('lottery').' where ptid = "ssc" and id = "'.$_GET['id'].'"');
if(!$info)
{
    ShowMsg('请重新选择彩种!',U());
}

$sscinfo = $db->getrow('select * from '.table('lottery_k3').' where tid = '.$info['id'].' and starttime <= '.time().' and stoptime >= '.time().'');
//if(!$sscinfo)
//{
  //  ShowMsg('当前彩票没有期号,请重新选择!',U('index/lotteryHall'));
//}

$data = date('m/d/Y H:i:s',$k3info['stoptime']);
// c被占用，这里在占用前赋值给临时变量

$type = $db->getall('select val,bl from '.table('lottery_type').' where type = "ssc" and types ="currency"');

$tpl->display('index/ssc');
